import { Action, Actor } from 'iflux2'
import { List } from 'immutable'

export default class NewsListActor extends Actor {

    defaultState() {
        return {
            status: false,// 是否是编辑模式(新增add或者编辑edit)
            curItem: {
                media_id: '',
                update_time: '',
                content: {
                    news_item: [
                        {
                            author: '',
                            content: '',
                            content_source_url: '',
                            digest: '',
                            need_open_comment: 0,
                            only_fans_can_comment: 0,
                            show_cover_pic: 0,
                            thumb_media_id: '',
                            thumb_url: '',
                            title: '',
                            url: ''
                        }
                    ]
                }
            },
            curIndexInItem: 0,
            edited: false
        }
    }

    @Action('EDITED_NEWS')
    editedNews(state, e) {
        return state.set('edited', !!e)
    }

    @Action('ADD_NEWS')
    addNews(state) {
        return state.set('status', 'add')
    }

    @Action('EDIT_NEWS')
    editNews(state) {
        return state.set('status', 'edit')
    }

    //编辑一条图文
    @Action('CHANGE_NEWS')
    changeNews(state, item) {
        return state.set('curItem', item)
    }

    @Action('QUIT_EDIT')
    quitEdit(state) {
        return state.set('status', false)
    }

    //修改一条图文中的 消息详情
    @Action('CHANGE_NEWS_VALUE')
    changeNewsValue(state, { tabs, index, key, value }) {
        if (tabs == 'news') {
            return state.setIn(['curItem', 'content', 'news_item', index, key], value)
        }
        return state.setIn(['curItem', 'articles', index, key], value)
    }

    //新增一条图文中的 消息详情
    @Action('ADD_NEWS_VALUE')
    editNewsValue(state, { tabs, item }) {
        if (tabs == 'news') {
            return state.updateIn(['curItem', 'content', 'news_item'], s => s.push(item))
        }
        return state.updateIn(['curItem', 'articles'], s => s.push(item))
    }

    //设置当前图文中 正在编辑的消息序号
    @Action('SET_NEWS_VALUE_INDEX')
    setNewsValueIndex(state, index) {
        return state.set('curIndexInItem', Math.min(index, (state.getIn(['curItem', 'content', 'news_item']) || state.getIn(['curItem', 'articles'])).size - 1));
    }

    @Action('INIT_EDIT_NEWS')
    initNewsList(state, list) {
        return state.update('newsList', s => list)
    }

    @Action('DELETE_NEWS')
    deleteNews(state, index) {
        if (state.getIn(['curItem', 'content', 'news_item'])) {
            return state.deleteIn(['curItem', 'content', 'news_item', index]);
        } else {
            return state.deleteIn(['curItem', 'articles', index]);
        }
    }

}